package STUDENT.TRINHQUANGXUANDUC.OPPS.dynamicarray.qlthuvien.Models;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import STUDENT.TRINHQUANGXUANDUC.OPPS.dynamicarray.qlthuvien.Entry.Bandoc;
import STUDENT.TRINHQUANGXUANDUC.OPPS.dynamicarray.qlthuvien.Entry.BangQLMuonSach;
import STUDENT.TRINHQUANGXUANDUC.OPPS.dynamicarray.qlthuvien.Entry.Sach;

public class Models {
	public int autoCode() {
		int code = 0;
		Random rad = new Random();
		code = rad.nextInt(50000);
		return code;
	}

	ObjectMapper objMapper;
	List<Bandoc> bandocs = new ArrayList<Bandoc>();
	List<BangQLMuonSach> qlMuonSachs = new ArrayList<BangQLMuonSach>();
	List<Sach> sachs = new ArrayList<Sach>();

	/****
	 * Phuong thuc nhap danh sach dau sach moi
	 * 
	 * @param sodausach
	 * @return
	 */
	public List<Sach> nhapDanhSachDauSach(Sach objSach) {
		Scanner sc = new Scanner(System.in);
		int masach = 0;
		int i = 0;
		System.out.println("-------Danh sach dau sach-------");
		while (i >= 0) {
			objSach = new Sach();
			masach = autoCode();
			System.out.println("Ma so sach:" + masach);
			objSach.setMasach(masach);
			System.out.println("Nhap ten sach:");
			objSach.setTensach(sc.nextLine());
			sc = new Scanner(System.in);
			System.out.println("Nhap ten tac gia sach:");
			objSach.setTacgia(sc.nextLine());
			sc = new Scanner(System.in);
			System.out.println("Nhap chuyen nganh cua sach: ");
			objSach.setChuyennganh(sc.nextLine());
			sc = new Scanner(System.in);
			System.out.println("Nam xuat ban: ");
			objSach.setNamxuatban(sc.nextInt());
			sachs.add(objSach);
			System.out.println("Ban co muon nhap tiep khong 1.Co/2.Khong ?");
			sc = new Scanner(System.in);
			int luachon = sc.nextInt();
			if (luachon == 1) {
				i++;
			} else {
				i = -1;
			}
			sc = new Scanner(System.in);
		}
		try {
			dataWriterJSON(2);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sachs;
	}

	/***
	 * Phuuong thuc nhap danh sach ban doc
	 * 
	 * @param sobandoc
	 * @return
	 */
	public List<Bandoc> Nhapdanhsachbandoc(Bandoc objBandoc) {
		Scanner sc = new Scanner(System.in);
		bandocs = new ArrayList<Bandoc>();
		int mabandoc = 0;
		int i = 0;
		System.out.println("Nhap danh sach ban doc");
		while (i >= 0) {
			objBandoc = new Bandoc();
			mabandoc = autoCode();
			System.out.println("Ma so ban doc: " + mabandoc);
			objBandoc.setMabandoc(mabandoc);
			System.out.println("Ho ten: ");
			sc = new Scanner(System.in);
			objBandoc.setHoten(sc.nextLine());
			System.out.println("Dia chi: ");
			sc = new Scanner(System.in);
			objBandoc.setDiachi(sc.nextLine());
			System.out.println("So dien thoai: ");
			sc = new Scanner(System.in);
			objBandoc.setSdt(sc.nextLine());
			System.out.println("Loai ban doc 1.Sinh vien/2.Hoc vien cao hoc/3.Giao vien");
			sc = new Scanner(System.in);
			objBandoc.setLoaibandoc(sc.nextInt());
			bandocs.add(objBandoc);
			System.out.println("Ban co muon nhap tiep khong 1.Co/2.Khong ?");
			sc = new Scanner(System.in);
			int luachon = sc.nextInt();
			if (luachon == 1) {
				i++;
			} else {
				i = -1;
			}
			sc = new Scanner(System.in);
		}
		try {
			dataWriterJSON(1);
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bandocs;
	}

	/***
	 * Phuong thuc in cac dau sach san co
	 * 
	 * @param arraysach
	 * @return
	 */
	public void getListSach() {
		System.out.println("Danh sach sach hien co");
		for (int i = 0; i < sachs.size(); i++) {
			System.out.println("---------********----------");
			System.out.println("Ma so sach: " + sachs.get(i).getMasach());
			System.out.println("Ten sach: " + sachs.get(i).getTensach());
			System.out.println("Tac gia: " + sachs.get(i).getTacgia());
			System.out.println("Chuyen nganh: " + sachs.get(i).getChuyennganh());
			System.out.println("Nam xuat ban: " + sachs.get(i).getNamxuatban());
		}
	}

	public void getListBanDoc() {
		System.out.println("Danh sach ban doc");
		for (int i = 0; i < bandocs.size(); i++) {
			System.out.println("---------********----------");
			System.out.println("Ma so ban doc: " + bandocs.get(i).getMabandoc());
			System.out.println("Ho ten: " + bandocs.get(i).getHoten());
			System.out.println("Dia chi: " + bandocs.get(i).getDiachi());
			System.out.println("So dien thoai: " + bandocs.get(i).getSdt());
			System.out.println("Loai ban doc: " + checkLoaiBanDoc(bandocs.get(i).getMabandoc()));
		}
	}

	/***
	 * Phuong thuc kiem tra loai ban doc
	 * 
	 * @param loai
	 * @return 1.Sinh vien 2. Sinh vien cao hoc 3.Giao vien
	 */

	public String checkLoaiBanDoc(int loai) {
		String kq = "";
		if (loai == 1) {
			kq = "Sinh vien";
		}
		if (loai == 2) {
			kq = "Sinh vien cao hoc";
		}
		if (loai == 3) {
			kq = "Giao vien";
		}
		return kq;
	}

	/***
	 * phuong thuc lay danh sach quan ly muon sach
	 */
	public void getListQuanlyMuonSach() {
		for (int i = 0; i < qlMuonSachs.size(); i++) {
			System.out.println("-------------**************-------------");
			System.out.println("Ma sach: " + qlMuonSachs.get(i).getSach().getMasach());
			System.out.println("Ma ban doc: " + qlMuonSachs.get(i).getBandoc().getMabandoc());
			System.out.println("Ten ban doc: " + qlMuonSachs.get(i).getBandoc().getHoten());
			System.out.println("Ten sach: " + qlMuonSachs.get(i).getSach().getTensach());
			System.out.println("Ngay muon: " + qlMuonSachs.get(i).getNgaymuon());
			System.out.println("So luong: " + qlMuonSachs.get(i).getSoluongsachmuon());
			System.out.println("Trang thai: " + checkTrangThaiMuonSach(qlMuonSachs.get(i).getTrangthai()));
		}
	}

	/***
	 * 
	 * Phuong thuc kiem tra trang thai muon sach
	 */
	public String checkTrangThaiMuonSach(int trangthai) {
		if (trangthai == 1) {
			return "Da tra";
		}
		return "Chua tra";

	}

	/***
	 * Phuong thuc sap xep quan ly muon sach option: 1.sap xep theo so luong
	 * muon sach,2.sap xep theo ten ban doc
	 * 
	 * @param list<>,
	 *            option
	 * @return list<>
	 */
	public List<BangQLMuonSach> sortQLMuonSach(List<BangQLMuonSach> array, int option) {
		List<BangQLMuonSach> temparaay = new ArrayList<BangQLMuonSach>();
		for (int i = 0; i < array.size(); i++) {
			temparaay.add(array.get(i));
		}
		if (option == 1) {
			BangQLMuonSach objtemp = new BangQLMuonSach();
			for (int i = 0; i < temparaay.size(); i++) {
				for (int j = i + 1; j < temparaay.size(); j++) {
					if (temparaay.get(i).getSoluongsachmuon() < temparaay.get(j).getSoluongsachmuon()) {
						objtemp = temparaay.get(i);
						temparaay.set(i, temparaay.get(j));
						temparaay.set(j, objtemp);
					}
				}
			}
		} else if (option == 2) {
			BangQLMuonSach objTemp = new BangQLMuonSach();
			for (int i = 0; i < temparaay.size(); i++) {
				for (int j = i + 1; j < temparaay.size(); j++) {
					String value1 = String.valueOf(temparaay.get(i).getBandoc().getHoten().charAt(0));
					String value2 = String.valueOf(temparaay.get(j).getBandoc().getHoten().charAt(0));
					if (value1.compareTo(value2) > 0) {
						objTemp = array.get(i);
						temparaay.set(i, temparaay.get(j));
						temparaay.set(j, objTemp);
					}
				}
			}
		}
		return temparaay;

	}

	/***
	 * Phuong thuc Nhap danh sach quan ly muon sach
	 * 
	 * @throws IOException
	 */
	public List<BangQLMuonSach> nhapDanhSachMuonSach(BangQLMuonSach objMuonSach)
			throws IOException, NumberFormatException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ngaymuon = format.format(calendar.getTime());
		boolean checkidreader = false;
		boolean checkidbook = false;
		int b = 0;
		while (b >= 0) {
			objMuonSach = new BangQLMuonSach();
			System.out.println("Nhap ma ban doc:");
			int mabandoc = Integer.parseInt(bf.readLine());
			for (Bandoc bandoc : bandocs) {
				if (mabandoc == bandoc.getMabandoc()) {
					/*
					 * objMuonSach.getBandoc().setMabandoc(mabandoc);
					 * objMuonSach.getBandoc().setHoten(bandoc.getHoten());
					 * objMuonSach.getBandoc().setDiachi(bandoc.getDiachi());
					 * objMuonSach.getBandoc().setLoaibandoc(bandoc.
					 * getLoaibandoc());
					 * objMuonSach.getBandoc().setSdt(bandoc.getSdt());
					 */
					objMuonSach.setBandoc(bandoc);
					checkidreader = true;
				}
			}
			if (checkidreader == true) {
				System.out.println("Nhap ma sach muon muon");
				int masach = Integer.parseInt(bf.readLine());
				for (Sach sach : sachs) {
					if (masach == sach.getMasach()) {
						checkidbook = true;
						objMuonSach.getSach().setMasach(sach.getMasach());
						objMuonSach.getSach().setTensach(sach.getTensach());
						objMuonSach.getSach().setTacgia(sach.getTacgia());
						objMuonSach.getSach().setChuyennganh(sach.getChuyennganh());
					}
				}
				if (checkidreader == true && checkidbook == true) {
					int soluong = 4;
					do {
						System.out.println("Nhap so luong sach muon");
						soluong = Integer.parseInt(bf.readLine());
						if (soluong >= 3) {
							System.out.println("Khong duoc muon qua 3 quyen");
						}
						objMuonSach.setSoluongsachmuon(soluong);
						System.out.println("Ngay muon:");
						System.out.println(ngaymuon);
						objMuonSach.setNgaymuon(ngaymuon);
						objMuonSach.setTrangthai(0);
					} while (soluong > 3);

				}
			}
			qlMuonSachs.add(objMuonSach);
			System.out.println("Ban co muon nhap tiep khong Y/N:");
			Scanner sc = new Scanner(System.in);
			String opt = sc.nextLine();
			if (opt.equals("N")) {
				b = -1;
			} else {
				b = 0;
			}

		}
		dataWriterJSON(3);
		return qlMuonSachs;

	}

	/***
	 * Phuong thuc tim kiem ban doc
	 * 
	 */
	public List<BangQLMuonSach> findBandocs(String tenbandoc) {
		List<BangQLMuonSach> listTemp = new ArrayList<BangQLMuonSach>();
		for (int i = 0; i < qlMuonSachs.size(); i++) {
			if (tenbandoc.equals(qlMuonSachs.get(i).getBandoc().getHoten())) {
				listTemp.add(qlMuonSachs.get(i));
			} else {
				listTemp = null;
			}
		}
		return listTemp;
	}

	/// Ghi du lieu vao file Json
	public void dataWriterJSON(int option) throws IOException, JsonGenerationException, JsonMappingException {
		objMapper = new ObjectMapper();
		if (option == 1) {
			objMapper.writeValue(new File("C:\\bandoc.json"), bandocs);
		}
		if (option == 2) {
			objMapper.writeValue(new File("C:\\sach.json"), sachs);
		}
		if (option == 3) {
			objMapper.writeValue(new File("C:\\muonsach.json"), qlMuonSachs);
		}

	}

	// Doc du lieu database Json
	public void loadData() throws IOException, JsonGenerationException, JsonMappingException {
		objMapper = new ObjectMapper();

		bandocs = objMapper.readValue(new File("C:\\bandoc.json"), new TypeReference<List<Bandoc>>() {
		});
		sachs = objMapper.readValue(new File("C:\\sach.json"), new TypeReference<List<Sach>>() {
		});
		List<BangQLMuonSach> temp = objMapper.readValue(new File("C:\\muonsach.json"),
				new TypeReference<List<BangQLMuonSach>>() {
				});
		for (BangQLMuonSach bangQLMuonSach : temp) {
			qlMuonSachs.add(bangQLMuonSach);
		}

	}
}
